﻿namespace UserStories.Attributes
{
    using System.Web.Mvc;

    using UserStories.Models;

    public class UserAttribute : ActionFilterAttribute
    {
        private readonly IProjectsService _projectsService;

        public UserAttribute() : this(FakeProjectsService.Instance())
        {
            
        }

        public UserAttribute(IProjectsService projectsService)
        {
            _projectsService = projectsService;
        }

        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            if (filterContext.HttpContext.Request.IsAuthenticated)
            {
                filterContext.Controller.ViewBag.User = _projectsService.User(filterContext.HttpContext.User.Identity.Name);
            }
        }
    }
}